package com.cql.cqaudio;

import android.graphics.ImageFormat;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cql.cqaudio.bean.TimeInfoBean;
import com.cql.cqaudio.listener.WlOnTimeInfoListener;
import com.cql.cqaudio.muterenum.MuterEnum;
import com.cql.cqaudio.utils.WlTimeUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "123123";
    private Wlplay wlplay;
    private TextView tvTime;
    private int position;
    private boolean isSeek = false;
    private SeekBar mSeekbar;
    private SeekBar volume;
    private TextView mVolume_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Example of a call to a native method
        Button tv = (Button) findViewById(R.id.sample_text);
        tvTime = (TextView) findViewById(R.id.text);
        mVolume_text = (TextView) findViewById(R.id.volume_text);
        Button pause = (Button) findViewById(R.id.pause);
        Button stop = (Button) findViewById(R.id.stop);
        Button resume = (Button) findViewById(R.id.resume);
        final Button seek = (Button) findViewById(R.id.seek);
        Button next = (Button) findViewById(R.id.next);
        Button right = (Button) findViewById(R.id.right);
        Button left = (Button) findViewById(R.id.left);
        Button center = (Button) findViewById(R.id.center);

        Button pitch = (Button) findViewById(R.id.pitch);
        Button tempo = (Button) findViewById(R.id.tempo);
        Button pitchAtempo = (Button) findViewById(R.id.pitchAtempo);
        Button normal = (Button) findViewById(R.id.normal);
        pitch.setOnClickListener(this);
        tempo.setOnClickListener(this);
        pitchAtempo.setOnClickListener(this);
        normal.setOnClickListener(this);
        right.setOnClickListener(this);

        left.setOnClickListener(this);
        center.setOnClickListener(this);
        mSeekbar = (SeekBar) findViewById(R.id.seekbar);
        volume = (SeekBar) findViewById(R.id.volume);
        wlplay = new Wlplay();
        wlplay.setVolume(50);
        volume.setProgress(wlplay.getVolume());
        mVolume_text.setText(wlplay.getVolume()+"");
        volume.setOnSeekBarChangeListener(volumeListener);
        mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (wlplay.getDuration() > 0 && isSeek) {
                    position = wlplay.getDuration() * progress / 100;
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isSeek = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //seekBar.setProgress(position);
                isSeek = false;
                wlplay.seek(position);
            }
        });
        seek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wlplay.seek(210);
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wlplay.pause();
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {wlplay.next("http://mpge.5nd.com/2015/2015-11-26/69708/1.MP3");
            }
        });
        resume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wlplay.resume();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wlplay.stop();
            }
        });
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {wlplay.onPrepared("http://mpge.5nd.com/2015/2015-11-26/69708/1.MP3");
            }
        });
        wlplay.setOnPreparedListener(new Wlplay.OnPreparedListener() {
            @Override
            public void onPrepared() {
                Log.e("onPrepared: ", "准备好了");
                wlplay.loadStart();
            }

            @Override
            public void error() {
                Log.e(TAG, "error: ");
            }

            @Override
            public void onLoad() {
                Log.e(TAG, "onLoad: ");
            }

            @Override
            public void resume() {
                Log.e(TAG, "resume: ");
            }

            @Override
            public void pause() {
                Log.e(TAG, "pause: ");
            }

            @Override
            public void stop() {
                Log.e(TAG, "stop: ");
            }

            @Override
            public void onComplete() {
                Log.e(TAG, "onComplete:");
                wlplay.stop();
            }
        });
        wlplay.setWlOnTImeinfoListener(new WlOnTimeInfoListener() {
            @Override
            public void onTimeLister(TimeInfoBean timeInfoBean) {
                Log.e(TAG, "onTimeLister: " + timeInfoBean.toString());

                Message message = Message.obtain();
                message.what = 1;
                message.obj = timeInfoBean;
                handler.sendMessage(message);


            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (!isSeek) {
                        TimeInfoBean timeInfoBean = (TimeInfoBean) msg.obj;
                        if (timeInfoBean != null)
                            tvTime.setText(WlTimeUtil.secdsToDateFormat(timeInfoBean.getTatolTime()) + "/" +
                                    WlTimeUtil.secdsToDateFormat(timeInfoBean.getCurrentTime()));
                        mSeekbar.setProgress(timeInfoBean.getCurrentTime() * 100 / timeInfoBean.getTatolTime());
                    }

                    break;
            }
        }
    };

 SeekBar.OnSeekBarChangeListener volumeListener=new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            mVolume_text.setText("音量"+progress);
            wlplay.setVolume(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.right:
                wlplay.set_muter(MuterEnum.MUTER_RIGHT);
                break;
            case R.id.left:
                wlplay.set_muter(MuterEnum.MUTER_LEFT);
                break;
            case R.id.center:
                wlplay.set_muter(MuterEnum.MUTER_CENTER);
                break;
            case R.id.pitch:
                wlplay.setPitchAndTempo(1.5f,1.0f);
                break;
            case R.id.tempo:
                wlplay.setPitchAndTempo(1.0f,1.5f);
                break;
            case R.id.pitchAtempo:
                wlplay.setPitchAndTempo(1.5f,1.5f);
                break;
            case R.id.normal:
                wlplay.setPitchAndTempo(1.0f,1.0f);
                break;

        }
    }
}
